package cn.lastjune.util;

import org.apache.commons.lang.StringUtils;

/**
 * Property Filter Helper Class
 * 
 * @author zhenyu lu
 */
public class PropertyFilter
{

	/**
	 * Multi compare condition separator
	 */
	public static final String OR_SEPARATOR = "_OR_";

	/**
	 * Property Filter Match Type: EQ, LIKE, LT, GT, LE, GE
	 * 
	 * @version 1.0
	 * @date Jan 15, 2010
	 * @author kevinhe
	 */
	public enum MatchType
	{
		/**
		 * equal
		 */
		EQ,
		/**
		 * like
		 */
		LIKE,
		/**
		 * like
		 */
		ILIKE,
		/**
		 * less than
		 */
		LT, /**
		 * greater than
		 */
		GT, /**
		 * less than or equal
		 */
		LE, /**
		 * greater than or equal
		 */
		GE;
	}

	private String[] propertyNames = null;
	private Object propertyValue = null;
	private MatchType matchType = null;

	/**
	 * Default empty constructor
	 */
	public PropertyFilter()
	{
	}

	/**
	 * @param propertyNames property nasmes
	 * @param propertyValue property value
	 * @param matchType match type
	 */
	public PropertyFilter(final String[] propertyNames, final Object propertyValue, final MatchType matchType)
	{
		this.propertyNames = propertyNames;
		this.propertyValue = propertyValue;
		this.matchType = matchType;
	}

	/**
	 * @param propertyNames property names
	 * @param propertyValue property value
	 * @param matchType match type
	 */
	public PropertyFilter(final String propertyNames, final Object propertyValue, final MatchType matchType)
	{
		this.propertyNames = StringUtils.split(propertyNames, PropertyFilter.OR_SEPARATOR);
		this.propertyValue = propertyValue;
		this.matchType = matchType;
	}

	/**
	 * @return Does there exist multi property names?
	 */
	public boolean isMultiProperty()
	{
		return propertyNames.length > 1;
	}

	/**
	 * @return the propertyNames
	 */
	public String[] getPropertyNames()
	{
		return propertyNames;
	}

	/**
	 * @return the property name when there only exist one
	 */
	public String getPropertyName()
	{
		if (propertyNames.length > 1)
		{
			throw new IllegalArgumentException("There are not only one property");
		}
		return propertyNames[0];
	}

	/**
	 * @param propertyNames the propertyNames to set
	 */
	public void setPropertyNames(final String[] propertyNames)
	{
		this.propertyNames = propertyNames;
	}

	/**
	 * @return the propertyValue
	 */
	public Object getPropertyValue()
	{
		return propertyValue;
	}

	/**
	 * @param propertyValue the propertyValue to set
	 */
	public void setPropertyValue(final Object propertyValue)
	{
		this.propertyValue = propertyValue;
	}

	/**
	 * @return the matchType
	 */
	public MatchType getMatchType()
	{
		return matchType;
	}

	/**
	 * @param matchType the matchType to set
	 */
	public void setMatchType(final MatchType matchType)
	{
		this.matchType = matchType;
	}
}
